//
//  NSMutableArray+THHook.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/14.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "NSMutableArray+THHook.h"
#import "THDeBugTool.h"
#import "THCrashManager.h"
@implementation NSMutableArray (THHook)
+ (void)load{
#ifdef DEBUG
    return;
#endif
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
           Class cls = NSClassFromString(@"__NSArrayM");
            SEL selectors[] = {
                @selector(objectAtIndex:),
                @selector(objectAtIndexedSubscript:),
                @selector(setObject:atIndexedSubscript:),
                @selector(removeObjectAtIndex:),
                @selector(insertObject:atIndex:),
                @selector(getObjects:range:),
            };
            SEL hookselectors[] = {
                @selector(avoidCrashObjectAtIndex:),
                @selector(avoidCrashObjectAtIndexedSubscript:),
                @selector(avoidCrashSetObject:atIndexedSubscript:),
                @selector(avoidCrashRemoveObjectAtIndex:),
                @selector(avoidCrashInsertObject:atIndex:),
                @selector(avoidCrashGetObjects:range:),
            };
            for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
                SEL originalSelector = selectors[index];
                SEL swizzledSelector = hookselectors[index];
                BOOL hook = ExchangeImplementationsInTwoClasses(cls,originalSelector,[NSMutableArray class], swizzledSelector);
                if (!hook) {
                    NSLog(@"拦截失败");
                }
            }
        });
}
#pragma mark - objectAtIndex:
- (id)avoidCrashObjectAtIndex:(NSUInteger)index {
    id object = nil;
    @try {
        object = [self avoidCrashObjectAtIndex:index];
    }@catch (NSException *exception) {
         [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
        return object;
    }
}
- (void)avoidCrashSetObject:(id)obj atIndexedSubscript:(NSUInteger)idx {
    @try {
        [self avoidCrashSetObject:obj atIndexedSubscript:idx];
    }@catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
    }@finally {}
}
#pragma mark - removeObjectAtIndex:

- (void)avoidCrashRemoveObjectAtIndex:(NSUInteger)index {
    @try {
        [self avoidCrashRemoveObjectAtIndex:index];
    }@catch (NSException *exception) {
       [[THCrashManager shareInstance] reportExp:exception];
    }@finally {}
}
#pragma mark - set方法
- (void)avoidCrashInsertObject:(id)anObject atIndex:(NSUInteger)index {
    @try {
        [self avoidCrashInsertObject:anObject atIndex:index];
    }@catch (NSException *exception) {
       [[THCrashManager shareInstance] reportExp:exception];
    }@finally {}
}
#pragma mark - objectAtIndexedSubscript:
- (id)avoidCrashObjectAtIndexedSubscript:(NSUInteger)idx {
    id object = nil;
    @try {
        object = [self avoidCrashObjectAtIndexedSubscript:idx];
    }@catch (NSException *exception) {
       [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
        return object;
    }
}
#pragma mark - getObjects:range:
- (void)avoidCrashGetObjects:(__unsafe_unretained id  _Nonnull *)objects range:(NSRange)range {
    @try {
        [self avoidCrashGetObjects:objects range:range];
    } @catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
    } @finally {
        
    }
}

@end
